﻿using System;
using Microsoft.Xna.Framework;

namespace Firefly.Framework.Input
{
    public class GamePadTriggerMap : InputMap, IPositionMapEx<float, GamePadManager, GamePadTriggerMapEventArgs>, IGamePadMap
    {
        public GamePadTrigger Trigger { get; private set; }
        
        public GamePadTriggerMap(PlayerIndex playerIndex, GamePadTrigger trigger)
            : this(playerIndex, trigger, string.Empty)
        {
        }

        public GamePadTriggerMap(PlayerIndex playerIndex, GamePadTrigger trigger, string action)
            : base()
        {
            PlayerIndex = playerIndex;
            InputMapType = InputMapType.GamePadTriggerMap;
            Trigger = trigger;
            Action = action;
            InputService.Instance.RegisterInputMap(this);
        }

        #region IPositionMap Members

        public float CurrentPosition
        {
            get
            {
                switch (Trigger)
                {
                    case GamePadTrigger.Left:
                        return Manager.CurrentLeftTriggerPosition;
                    case GamePadTrigger.Right:
                        return Manager.CurrentRightTriggerPosition;
                    default:
                        return 0.0f;

                }
            }
        }
        public float PreviousPosition
        {
            get
            {
                switch (Trigger)
                {
                    case GamePadTrigger.Left:
                        return Manager.PreviousLeftTriggerPosition;
                    case GamePadTrigger.Right:
                        return Manager.PreviousRightTriggerPosition;
                    default:
                        return 0.0f;
                }
            }
        }
        public float PositionDelta
        {
            get
            {
                switch (Trigger)
                {
                    case GamePadTrigger.Left:
                        return Manager.LeftTriggerPositionDelta;
                    case GamePadTrigger.Right:
                        return Manager.RightTriggerPositionDelta;
                    default:
                        return 0.0f;
                }
            }
        }

        #endregion

        #region IPositionMapEx Members

        public event EventHandler<GamePadTriggerMapEventArgs> PositionChanged;

        private void NotifyPositionChanged()
        {
            EventHandler<GamePadTriggerMapEventArgs> handler = PositionChanged;

            if (handler != null)
            {
                handler(this, new GamePadTriggerMapEventArgs(this));
            }
        }

        #endregion

        #region IHasInputManager Members

        public GamePadManager Manager { get; set; }

        #endregion

        #region IUpdateable Members

        public override void Update(GameTime gameTime)
        {
            if (Enabled)
            {
                if (PositionDelta > 0.0f)
                {
                    NotifyPositionChanged();
                }
            }
        }

        #endregion

        #region IGamePadMap Members

        public PlayerIndex PlayerIndex { get; private set; }

        #endregion
    }
}
